package com.example.service;

import com.example.dao.NodeDAOMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Transactional
public class NodeServiceImpl {

    @Resource
    private NodeDAOMapper nodeMapper;


    public List<Map<String, Object>> getGamePoint() {
        List<Map<String, Object>> codeList = nodeMapper.getGamePoint();
        return orgRecursion(codeList, "0");
    }

    /**
     * 递归获取某个父机构节点下面的所有子机构节点
     */
    private List<Map<String, Object>> orgRecursion(List<Map<String, Object>> codeList, String parentId) {
        List<Map<String, Object>> firstList = codeList.stream().filter(map -> parentId.equals(map.get("parent_id") + "")).collect(Collectors.toList());
        List<Map<String, Object>> resultList = new ArrayList<>();
        for (Map<String, Object> map : firstList) {
            List<Map<String, Object>> childList = codeList.stream().filter(obj -> (map.get("order_id") + "").equals(obj.get("parent_id") + "")).collect(Collectors.toList());
            map.put("childNodes", childList);
            orgRecursion(codeList, map.get("order_id") + "");
            resultList.add(map);
        }
        return resultList;
    }

}
